home *** CD-ROM | disk | FTP | other *** search
/ Executor 2.0 / executorv2.0.iso / mac / demoware / Asymptote 1.1 Demo / Scripts / Demos / 13) Damped Sine / demo13 < prev   
Encoding:
Text File  |  1994-11-01  |  1.5 KB  |  70 lines  |  [TEXT/DWat]

  1. % A demonstration of curve fitting
  2. % in Asymptote.
  3.  
  4. % Fit a damped sine function
  5. %    y = a1*exp(-a0*x)*sin(a2*x + a3).
  6.  
  7. % Open the data file and read x and y
  8. datafile Damped Sine Data
  9. datalines 2
  10. readcolumn x 1
  11. readcolumn y 2
  12. rpn 3 >e        % Set all the errors to 3            
  13. hidewindow \f
  14. fullscreen on
  15. % Find the limits and plot the data with error bars
  16. graphlimits
  17. pointsize 4
  18. pointsymbol 4 filled
  19. plotpoints
  20. % Draw a frame and label the axes
  21. drawframe
  22. ploterrors top
  23. ploterrors bottom
  24. scale 1.5
  25. drawylabel K\d1e\\u-\sat\\dsin(\swt + \sf)
  26. drawxlabel t
  27.  
  28. % Fit the function
  29. %    y = a1*exp(-a0*x)*sin(a2*x + a3)
  30. % where x is in degrees
  31.  
  32. % Parameter initial values:
  33. set a0 0.15    % exponential factor
  34. set a0fixed no
  35. set a1 80    % exponential amp.
  36. set a1fixed no
  37. set a2 10      % sine frequency
  38. set a2fixed no
  39. set a3 0      % sine phase
  40. set a3fixed no
  41.  
  42. % Specify that we want to see each iteration
  43. set plot_each_fit yes
  44.  
  45. % Fit the function
  46. fitxy 4 a0 x * chs e^x a1 * x a2 * a3 + deg sin *
  47.  
  48. % Calculate the fit at each x
  49. rpn a0 <x * chs e^x a1 * <x a2 * a3 + deg sin * >y
  50.  
  51. % Plot the fit on top of the data with a thicker pen
  52. pensize 1
  53. plotline
  54.  
  55. % Move to the bottom right corner of the graph (this is a macro)
  56. BR
  57. % Draw the label a bit bigger
  58. scale 1.25
  59. % Use 3 significant digits for labels
  60. digits 3
  61. % Label the plot with the fit results
  62. drawlabel centerleft \sa:\a0   \sw:\a2   \sf:\a3    K\d1:\a1   \sc\u2:\chisq
  63. % Draw a title including the name of the data file and the date
  64. fontsize 14
  65. drawtitle  Curve Fit To '\f' on \#
  66.  
  67. % Close the Data Window
  68. closewindow \f
  69.  
  70. fullscreen no